package com.gopay.common.cps.dao.wallet.bouns.impl;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.cps.dao.wallet.bouns.WaBounsDetailOrderDao;
import com.gopay.common.cps.dao.wallet.checkSms.impl.WaCheckSmsDaoImpl;
import com.gopay.common.domain.dps.WaBounsDetailOrder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 
 * @author boai 
 * on 2017/1/5.
 */
@Repository("waBounsDetailOrderDao")
public class WaBounsDetailOrderDaoImpl extends BaseDAOImpl<WaBounsDetailOrder, Integer> implements WaBounsDetailOrderDao {
    protected Logger logger = LoggerFactory.getLogger(WaCheckSmsDaoImpl.class);
    @Override
    public WaBounsDetailOrder findBestLuckByBounsMainOrderId(String mainOrderId){
//       select *
//       from wa_bouns_detail_order r
//       where r.BOUNS_MAIN_ORDER_ID = ''
//       and r.BOUNS_AMT = (select max(r.BOUNS_AMT)
//       from wa_bouns_detail_order m
//       where r.BOUNS_MAIN_ORDER_ID = '')
//       order by r.RECV_TIME desc
       StringBuffer sb=new StringBuffer();
       sb.append(" from WaBounsDetailOrder r ");
       sb.append(" where r.bounsMainOrderId= ");
       sb.append(mainOrderId);
       sb.append(" and r.bounsAmt = ");
       sb.append(" (select max(m.bounsAmt) from WaBounsDetailOrder m where m.bounsMainOrderId = ");
       sb.append(mainOrderId+") ");
       sb.append(" order by  r.recvTime ");
        logger.info(sb.toString());
       List<WaBounsDetailOrder> waBounsDetailOrder=this.findByHql(sb.toString());
       if(waBounsDetailOrder==null || waBounsDetailOrder.size() == 0){
    	   return null;
       }
            return waBounsDetailOrder.get(0);
    }
    @Override
    public WaBounsDetailOrder findbyGopayOrderId(String gopayOrderId){
        String hql = " from WaBounsDetailOrder w where w.gopayOrderId =:gopayOrderId";
        Map<String, Object> map = new HashMap<>();
        map.put("gopayOrderId", gopayOrderId);
        List<WaBounsDetailOrder> waBounsDetailOrderList = this.findByHql(hql, map);
        return waBounsDetailOrderList!=null?waBounsDetailOrderList.get(0):null;
    }
    @Override
    public List<WaBounsDetailOrder> findbyBounsMainOrderId(Integer bounsMainOrderId){
        String hql = " from WaBounsDetailOrder w where w.bounsMainOrderId =:bounsMainOrderId and w.bounsStatus in (2,3) order by w.recvBounsMax ,w.recvTime desc";
        Map<String, Object> map = new HashMap<>();
        map.put("bounsMainOrderId", bounsMainOrderId);
        return this.findByHql(hql, map);
    }
}
